package org.lg.studyspringboot20.config;


//import com.interceptor.PageInterceptor;

//import org.apache.ibatis.plugin.Interceptor;
//import com.interceptor.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

// mapper接口  mapper xml  entity
//mapperScan扫描指定文件夹下面的接口,sqlSessionFactoryRef 数据库连接点的工厂找谁要
@Configuration
@MapperScan(basePackages = {"org.lg.studyspringboot20.mapper"},sqlSessionFactoryRef = "sqlSessionFactory")
public class MyBatisConfig {



    // 工厂生产要dataSource 由spring产生
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();//工厂bean的对象是产品
        sessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //扫描 数据库和java entity 对应的配置文件，所在包是哪里
        sessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/org/lg/studyspringboot20/mapper/*Mapper.xml"));       //mybatis的xml文件路径
        sessionFactoryBean.setTypeAliasesPackage("org.lg.studyspringboot20.entiy");//生成的实体类映射在那个包
//        //将分页拦截器注册到插件中
        //PageInterceptor pageInterceptor = new PageInterceptor();
        //sessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor});
        return  sessionFactoryBean;
    }

}
